package com.sxfq.loansupermarket.backsystem.modular.theTest.controller;

import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.UserMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TestQuestionsInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.UserForTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ICompletedTestService;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.IInTestService;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITestingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 考试进行中
 *
 * @author py
 * @Date 2018年2月4日23:05:54
 */
@Controller
@RequestMapping("/testing")
public class TestingController extends BaseController {

    @Autowired
    IInTestService iInTestService;

    @Autowired
    ITestingService iTestingService;

    @Autowired
    ICompletedTestService completedTestService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private SqlMapper sqlMapper;


    /**
     * 跳转考试内容页
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String TestingPost(Model model) {
        String theTestId = super.getPara("theTestId").trim();
        TheTestInfo theTestInfo = iTestingService.testGenerate(theTestId);
        ShiroKit.setSessionAttr("theTestName", theTestInfo.getTheTestName());
        ShiroKit.setSessionAttr("scoresOfAdoption", theTestInfo.getScoresOfAdoption());
        ShiroKit.setSessionAttr("timeLength", theTestInfo.getTimeLength());
        ShiroKit.setSessionAttr("totalScore", getDailyInterestRate(theTestInfo.getTotalScore().toString()));
        ShiroKit.setSessionAttr("numberOfQuestion", theTestInfo.getNumberOfQuestion());
        ShiroKit.setSessionAttr("scoresOfQuestion", getDailyInterestRate(theTestInfo.getScoresOfQuestion().toString()));
        ShiroKit.setSessionAttr("theTestId", theTestId);
        return "/theTest/testing.html";
    }

    /**
     * 跳转考试内容页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String Testing(Model model) {
        List<UserForTestInfo> list = completedTestService.selectDetails(ShiroKit.getSessionAttr("theTestId"), ShiroKit.getUser().getId());
        long remainingTime = 0;
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(list.get(0).getStartTime());
        Calendar currentTime = Calendar.getInstance();
        currentTime.setTime(new Date());
        long startSecond = startTime.getTimeInMillis();
        long currentSecond = currentTime.getTimeInMillis();
        long totleSecond = Integer.parseInt(ShiroKit.getSessionAttr("timeLength")) * 60;
        remainingTime = (totleSecond - (currentSecond - startSecond) / 1000);
//        remainingTime = Integer.parseInt(ShiroKit.getSessionAttr("timeLength")) * 60;
        if (remainingTime >= 0) {
            model.addAttribute("remainingTime", remainingTime);
            model.addAttribute("times", formatTime(remainingTime));
        } else {
            model.addAttribute("remainingTime", 0);
            model.addAttribute("times", "00:00:00");
        }
        List<TestQuestionsInfo> getList=iTestingService.getList(ShiroKit.getSessionAttr("theTestId"), ShiroKit.getUser().getId());
        model.addAttribute("testQuestionsInfoListl", getList.size());
        model.addAttribute("testQuestionsInfoList", getList);
        model.addAttribute("password", ShiroKit.getSessionAttr("password"));
        model.addAttribute("totalScore", ShiroKit.getSessionAttr("totalScore"));
        model.addAttribute("theTestId", ShiroKit.getSessionAttr("theTestId"));
        model.addAttribute("numberOfQuestion", ShiroKit.getSessionAttr("numberOfQuestion"));
        model.addAttribute("scoresOfQuestion", ShiroKit.getSessionAttr("scoresOfQuestion"));
        model.addAttribute("theTestName", ShiroKit.getSessionAttr("theTestName"));
        model.addAttribute("timeLength", ShiroKit.getSessionAttr("timeLength"));
        Integer id = ShiroKit.getUser().getId();
        User user = userMapper.selectById(id);
        model.addAttribute("userName", user.getName());
        model.addAttribute("userId", ShiroKit.getUser().getId());
        return "/theTest/testing.html";
    }

    public String formatTime(Long totleSecond) {
        Long hour = totleSecond / 3600;
        Long minute = (totleSecond % 3600) / 60;
        Long second = totleSecond % 60;
        String hours = formatTimeNumber(hour);
        String minutes = formatTimeNumber(minute);
        String seconds = formatTimeNumber(second);
        return hours + ":" + minutes + ":" + seconds;
    }

    public String formatTimeNumber(Long number) {
        if (number < 10) {
            return "0" + number;
        } else {
            return number+"";
        }
    }

    @RequestMapping(value = "/funs", method = RequestMethod.GET)
    @ResponseBody
    public List funs(Model model) {
//        List<TestQuestionsInfo> getList = iTestingService.getList(ShiroKit.getSessionAttr("theTestId"), ShiroKit.getUser().getId());
//        model.addAttribute("testQuestionsInfoListl", getList.size());
//        model.addAttribute("testQuestionsInfoList", getList);
        String sql="select * from test_questions_info";
        List<TestQuestionsInfo> testQuestionsInfoList = sqlMapper.selectList(sql, TestQuestionsInfo.class);
        return testQuestionsInfoList;
    }

    private String getDailyInterestRate(String dailyInterestRate) {
        if (dailyInterestRate.indexOf(".") > 0) {
            //正则表达
            dailyInterestRate = dailyInterestRate.replaceAll("0+?$", "");//去掉后面无用的零
            dailyInterestRate = dailyInterestRate.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return dailyInterestRate;
    }

    /**
     * 跳转到选择页面
     */
    @RequestMapping("/answerList/{id}")
    public String toAnswerList(@PathVariable String id, Model model) {
        UserForTestInfo info = iTestingService.selectOneById(id);
        ShiroKit.setSessionAttr("questionId", info.getTestQuestionsId());
        ShiroKit.setSessionAttr("userForTestId", id);
        model.addAttribute("questionId", info.getTestQuestionsId());
        model.addAttribute("userForTestId", id);
        model.addAttribute("theTestId", ShiroKit.getSessionAttr("theTestId"));
        return "/theTest/answerList.html";
    }

}
